package org.example;

public class Test3 {
    //leetcode 541 反转字符串 II https://leetcode.cn/problems/reverse-string-ii/description/
    public void reverse(char[] arr, int left, int right) {
        while (left < right) {
            char tmp = arr[left];
            arr[left++] = arr[right];
            arr[right--] = tmp;
        }
    }

    public String reverseStr(String s, int k) {
        char[] arr = s.toCharArray();
        int left = 0, right = 0, n = arr.length;
        while (right < n) {
            right += 2 * k;
            if (left + k > n - 1) {
                reverse(arr, left, n - 1);
            }else {
                reverse(arr,left,left + k - 1);
            }
            left = right;
        }
        return String.valueOf(arr);
    }

    public static void main(String[] args) {
        Test3 test3 = new Test3();
        String s = "abcdefg";
        test3.reverseStr(s,2);
        System.out.println(666);
    }
}
